package com.zhuliwei.controller;

import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.Captcha;
import com.zhuliwei.domain.entity.User;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.exception.SystemException;
import com.zhuliwei.service.BlogAuthService;
import com.zhuliwei.service.impl.CaptchaServiceImpl;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * 认证接口控制层
 *
 * @author 朱力维
 * @time 2022/10/13 12:27
 **/
@RestController
@Api(tags="认证",description = "登录相关接口")
public class BlogAuthController {
    @Resource
    private BlogAuthService blogAuthService;

    @Resource
    private CaptchaServiceImpl captchaService;

    @PostMapping("get-captcha")
    public ResponseResult getCaptcha(@RequestBody Captcha captcha) {
        return ResponseResult.okResult(captchaService.getCaptcha(captcha));
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user, HttpServletRequest request){
        if (!StringUtils.hasText(user.getUserName())){
           //提示 必须要传用户名
           throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
       }else if (!StringUtils.hasText(user.getPassword())){
           throw new SystemException(AppHttpCodeEnum.REQUIRE_PASSWORD);
       }
       return blogAuthService.login(user,request);
    }
    @PostMapping("/logout")
    public ResponseResult logout(){
      return blogAuthService.logout();
    }
}
